# -*- coding: utf-8 -*-
"""
@Author: thekernel
@Date: 2020/2/16 22:19
@Description: 从单条答案生成多个类型答案(细分标准)
"""
import xlrd
import xlwt
import csv
import math

excel_list = list()
keyword_list = list()
excel_to_keyword = dict()
data = list()
removable = {
    "、、": "、",
    "；；": "；",
    "；。": "。",
    "、。": "。",
    "：、": "：",
    "，、": "，",
    "：。": "。"
}


def read_excel(file: str):
    work_sheet = xlrd.open_workbook(file)
    sheet = work_sheet.sheet_by_index(0)
    row_num = sheet.nrows

    for i in range(1, row_num):
        excel_list.append(dict({
            "id": sheet.cell(i, 0).value,
            "question": sheet.cell(i, 1).value,
            "answer": sheet.cell(i, 2).value,
            "0": sheet.cell(i, 3).value,
            "10": sheet.cell(i, 4).value
        }))


def read_keyword(file: str):
    with open(file, "r", encoding="GBK") as f:
        reader = csv.reader(f)

        for i, row in enumerate(reader):
            if 115 <= i <= 240:
                keyword_list.append(row[0].split("，"))


def excel_map_keyword(l1, l2):
    for x, excel in enumerate(l1):
        for y, keywords in enumerate(l2):
            flag = True
            for keyword in keywords:
                if keyword not in excel["answer"]:
                    flag = False
                    break
            if flag:
                excel_to_keyword[x] = y
                break


def remove_empty(s: str):
    for k, v in removable.items():
        while k in s:
            s = s.replace(k, v)

    return s


def generate_result():
    for key, value in excel_to_keyword.items():
        tmp_data = list()
        excel = excel_list[key]["answer"]
        keywords = keyword_list[value]
        # keywords.reverse()
        keywords = keywords[: 10]
        total = len(keywords)
        step = math.floor(10 / total)
        tmp = [
            {
                "text": excel_list[key]["id"],
                "score": -3
            },
            {
                "text": excel_list[key]["question"],
                "score": -2
            },
            {
                "text": excel_list[key]["answer"],
                "score": -1
            },
            {
                "text": excel,
                "score": 10
            },
        ]
        tmp_data.extend(tmp)
        for i in range(total):
            score = step * (total - i - 1)
            excel = excel.replace(keywords[i], "")
            while "、、" in excel:
                excel = excel.replace("、、", "、")
            while "；；" in excel:
                excel = excel.replace("；；", "；")
            while "、。" in excel:
                excel = excel.replace("、。", "。")
            tmp = dict({
                "text": excel,
                "score": score
            })
            if score == 0:
                tmp = {
                    "text": excel_list[key]["0"],
                    "score": 0
                }
            elif score == 10:
                tmp = {
                    "text": excel_list[key]["10"],
                    "score": 10
                }
            tmp_data.append(tmp)
        data.append(tmp_data)


def save_result():
    workbook = xlwt.Workbook(encoding='utf-8')
    sheet1 = workbook.add_sheet("Sheet1")
    sheet1.write(0, 0, "题号")
    sheet1.write(0, 1, "问题")
    sheet1.write(0, 2, "标准答案")
    # sheet1.write(0, 3, "0")
    # sheet1.write(0, 4, "1")
    # sheet1.write(0, 5, "2")
    # sheet1.write(0, 6, "3")
    # sheet1.write(0, 7, "4")
    # sheet1.write(0, 8, "5")
    # sheet1.write(0, 9, "6")
    # sheet1.write(0, 10, "7")
    # sheet1.write(0, 11, "8")
    # sheet1.write(0, 12, "9")
    # sheet1.write(0, 13, "10")
    sheet1.write(0, 3, "得分点")
    for i, row_data in enumerate(data):
        for d in row_data:
            try:
                sheet1.write(i + 1, d["score"] + 3, d["text"])
            except Exception as e:
                print(e)

    workbook.save(r'data/121-240-3.xlsx')


if __name__ == '__main__':
    excel_file = "data/121-240.xlsx"
    keyword_file = "data/keyword.csv"

    read_keyword(keyword_file)
    read_excel(excel_file)

    excel_map_keyword(excel_list, keyword_list)

    generate_result()
    save_result()
